/**
 * 菜单树，主要用于显示树结构的菜单数据，带右键菜单功能
 * json格式：[{"id":"OmOrg#10","leaf":false,...},{...},...]
 * java类：com.augurit.common.system.common.MenuTreeNode 
 * @class Ext.ux.MenuTree
 * @extends Ext.tree.TreePanel
 */
Ext.namespace("Ext.ux");

Ext.ux.MenuTree = Ext.extend(Ext.tree.TreePanel, {

	// 获取数据的URL
	url: '',
	
	// 是否使用箭头图标
	useArrows: true,
	
	// 是否显示动画
	animate: true,
	
	// 是否允许拖放
	enableDD: true,
	
	// 是否显示边框
	border: false,
	
	// 是否显示根节点
	rootVisible: true,
	
	// 是否自动显示滚动条
	autoScroll: true,
	containerScroll: true,
	
	// 设置间隔样式
	bodyStyle: 'padding:3px',

	// 组件初始化
	initComponent: function(){
	
		// 根节点
    	this.root = new Ext.tree.AsyncTreeNode({
			id: "-1",			// 根节点的默认ID为“-1”
			text: '菜单树',
			draggable: false,	// 不允许拖放
			leaf: false,		// 非叶子节点
			iconCls: 'icon-tree-root'
		});
		
		// 数据加载器
		this.loader = new Ext.tree.TreeLoader({
			dataUrl: this.url,
			listeners: {
				"beforeload": function(treeLoader, node){
					treeLoader.baseParams.id = node.id;
				}
			}
		});
		
		// 调用父类初始化
		Ext.ux.MenuTree.superclass.initComponent.call(this);
		
		
		// 定义根节点的右键菜单
		var rootMenu = new Ext.menu.Menu({
			items: [{
				text: '新增顶级菜单',
				iconCls: 'add-child-menu',
				handler: this.addTopMenu,
				scope: this
			},'-',{
				text: '刷新',
				iconCls: 'refresh',
				handler: this.refreshMenu,
				scope: this
			}]
		});
		
		// 定义非叶子菜单节点的右键菜单
		var parentMenu = new Ext.menu.Menu({
			items: [{
				text: '新增子菜单',
				iconCls: 'add-child-menu',
				handler: this.addChildMenu,
				scope: this
			},'-',{
				text: '删除菜单',
				iconCls: 'delete-menu',
				handler: this.deleteMenu,
				scope: this
			},'-',{
				text: '刷新',
				iconCls: 'refresh',
				handler: this.refreshMenu,
				scope: this
			}]
		});
		
		
		// 定义叶子菜单节点的右键菜单
		var leafMenu = new Ext.menu.Menu({
			items: [{
				text: '删除菜单',
				iconCls: 'delete-menu',
				handler: this.deleteMenu,
				scope: this
			}]
		});
		
		
		// 注册点击事件
		this.on('beforeclick', function(node, e){
			if(node.id == -1){
				this.queryMenu();
			}else{
				this.loadMenuInfo(node.id);
			}
		}, this);
		
		// 注册右键菜单及菜单实现
		this.on('contextmenu', contextMenu, this);
		function contextMenu(node, e) {
			e.preventDefault();
			node.select();
			if(node.id == '-1'){
				rootMenu.showAt(e.getXY());
			}else{
				if(!node.leaf){
					parentMenu.showAt(e.getXY());
				}else if(node.leaf){
					leafMenu.showAt(e.getXY());
				}
			}
		}
	
		// 展开根节点
		this.getRootNode().expand();
		
		
	},
	
	//节点刷新
	refreshMenu: function(){
		var node = this.getSelectionModel().getSelectedNode();
		if(node == null || node.id == -1){
			this.root.reload();
		}else{
			node.parentNode.reload();
		}
	},

	//加载菜单查询面板
	queryMenu: function(){
		Ext.getCmp('mainDetail').setActiveTab(0);
		Ext.getCmp('menuQueryPanel').loadData();
	},
	
	//加载菜单详细信息面板
	loadMenuInfo: function(menuId){
		var mainDetail = Ext.getCmp('mainDetail');
	
		var menuDetailPanel = Ext.getCmp('menuDetailPanel');
		if(menuDetailPanel == null){
			menuDetailPanel = new Ext.ux.org.acmenu.MenuDetailPanel({id:'menuDetailPanel', title:'菜单详细信息'});
		}
		mainDetail.add(menuDetailPanel);
		
		mainDetail.setActiveTab(1);
		menuDetailPanel.loadMenu(menuId);
	},

	//删除菜单
	deleteMenu: function() {
		Ext.MessageBox.show({
			title: '系统提示',
			msg: '确认要删除选中的菜单吗？<br/><font color="red">注意</font>：删除菜单将同时删除与菜单相关的功能、快捷菜单关联信息。',
			buttons: Ext.Msg.YESNO,
			icon: Ext.MessageBox.QUESTION,
			fn: this.confirmDeleteMenu,
			width: 390,
			scope: this
		});
	},
	
	//执行删除菜单操作
	confirmDeleteMenu: function(btn) {
		if(btn == 'yes'){
			var menuId = this.getSelectionModel().getSelectedNode().id;
			Ext.Ajax.request({
				url: 'ac-menu!delete.action',
				success: function(){
					Ext.getCmp('menuTree').refreshMenu();
					//this.store.reload();
			},
			failure: function(form, action) {
				Ext.Msg.alert('系统提示', '删除菜单失败！');
			},
			method: 'POST',
			params: {
				ids: menuId
			},
			waitMsg: '正在删除数据，稍候...',
			scope: this
			});
		}
		
	},
	
	//新增顶级菜单
	addTopMenu: function(){
		var win = new Ext.ux.org.acmenu.MenuDetailWindow({title:'新增顶级菜单'});
		win.show();
	},
	
	//新增子菜单
	addChildMenu: function(){
		var selNode = this.getSelectionModel().getSelectedNode();
		var parentMenuId = selNode.attributes.id;
		var parentMenuName = selNode.text;
			
		var win = new Ext.ux.org.acmenu.MenuDetailWindow({title:'新增子菜单', parentMenuId:parentMenuId, parentMenuName:parentMenuName});
		win.addChildMenu();
	}
});